import axios from "@/util/axios";
import { message } from "antd";
import { Dispatch, RootState } from "@/store";
import { Res } from "./index.type";
import qs from "qs";

interface State {}

interface Params {
  pageNum: number;
  pageSize: number;
  id?: number | string;
  name?: string;
}

const defaultState: State = {
  cardDatas: [],
  total: 0,
};

export default {
  state: defaultState,
  reducers: {},

  effects: (dispatch: Dispatch) => ({
    async getData(params: Params) {
      try {
        const res: Res = await axios.get(
          `/api/getCards?${qs.stringify(params)}`
        );
        return res;
      } catch (err) {
        message.error("网络请求失败，请重试");
      }
      return;
    },

    async add(params: any) {
      try {
        const res: Res = await axios.post("/card/add", params);
        return res;
      } catch (err) {
        message.error("网络请求失败，请重试");
      }
      return;
    },

    async update(params: any) {
      try {
        const res: Res = await axios.post("/card/update", params);
        return res;
      } catch (err) {
        message.error("网络请求失败，请重试");
      }
      return;
    },

    async delete(id: number | string) {
      try {
        const res: Res = await axios.post("/card/delete", { id });
        return res;
      } catch (err) {
        message.error("网络请求失败，请重试");
      }
      return;
    },
  }),
};
